﻿using System;
using Microsoft.SharePoint;

namespace Expertime.Multitude.Intranet.Utils
{
    public class SPListItemElementUtility
    {
        #region const
        /// <summary>
        /// The generic display name of every item. It may have a different display name in different lists but the same internal field name of Title. The title is the default text to display for Lookup fields to lists.
        /// </summary>
        public const String FIELD_TITLE = "Title";
        public const String FIELD_DESCRIPTION = "Description";
        /// <summary>
        /// 	A computed field that displays hyperlinks to items, containing the title texts and generating URLs (and new icon references) to the items. There is no additional storage for this field; it relies on the ID and Title fields for rendering of its UI.
        /// </summary>
        public const String FIELD_LINKTITLE = "LinkTitle";
        public const String FIELD_UNIQUE_ID = "UniqueId";
        public const String FIELD_CONTENT_TYPE = "ContentType";
        public const String FIELD_CONTENT_TYPE_ID = "ContentTypeId";
        /// <summary>
        /// A field that contains a DateTime value specifying the time that the item was created (in UTC).
        /// </summary>
        public const String FIELD_CREATED = "Created";

        /// <summary>
        /// A field that contains a Lookup value that refers to the UserInfo table and indicates the user who created the item.
        /// </summary>
        public const String FIELD_CREATED_BY = "Author";//"CreatedBy";
        /// <summary>
        /// A field that contains a DateTime value specifying the time that the item was last modified (in UTC). The Modified time is used as a sort of Version field as well. The last modification date of an item is always posted when an existing item is being edited. If it does not match the current modification date, the server fails the edit and returns an error to the user, providing a link to the current data on the server (which opens in a new window), so that posted values can be copied from the old form into the new one.
        /// </summary>
        public const String FIELD_MODIFIED = "Modified";
        /// <summary>
        /// A field that contains a Lookup value to the UserInfo table indicating the user who last modified the item.
        /// </summary>
        public const String FIELD_MODIFIED_BY = "ModifiedBy";
        public const String FIELD_AUTHOR = "Author";
        public const String FIELD_PUBLISHED = "Published";
        /// <summary>
        /// A Computed field (with no additional schema) that contains a hyperlink to the form for editing an item. This field references the page for editing the item and the ID field to identify it.
        /// </summary>
        public const String FIELD_EDIT = "Edit";

        /// <summary>
        /// Date de l'évènement (début)
        /// </summary>
        public const String FIELD_EVENT_DATE = "EventDate";
        /// <summary>
        /// Date de fin de l'évènement
        /// </summary>
        public const String FIELD_EVENT_ENDDATE = "EndDate";
        public const String FIELD_EVENT_RECURRENCEID = "RecurrenceID";
        /// <summary>
        /// Emplacement
        /// </summary>
        public const String FIELD_EVENT_LOCATION = "Location";
        /// <summary>
        /// Booléen: répétition de l'évènement
        /// </summary>
        public const String FIELD_EVENT_FRECURRENCE = "fRecurrence";
        /// <summary>
        /// Description technique de la périodicité
        /// </summary>
        public const String FIELD_EVENT_RECURRENCEDATA = "RecurrenceData";
        /// <summary>
        /// Booléean: journée entière 
        /// </summary>
        public const String FIELD_EVENT_FALLDAYEVENT = "fAllDayEvent";
        /// <summary>
        /// Type
        /// </summary>
        public const String FIELD_EVENT_TYPE = "EventType";

        public const String FIELD_MODERATION_STATUS = "_ModerationStatus";

        #endregion

        #region Methods
        public static T GetItemValue<T>(SPListItem item, string fieldName, T defaultValue)
        {
            try
            {
                if (item != null && item.Fields !=null && item.Fields.ContainsField(fieldName))
                {
                    if (typeof(T).IsEnum)
                        return (T)Enum.Parse(typeof(T), GetItemValue<string>(item, fieldName, defaultValue.ToString()));
                    else
                        return (T)Convert.ChangeType(item[fieldName] ?? defaultValue, typeof(T));
                }
                else
                    return defaultValue;
            }
            catch { return defaultValue; }
        }
        public static string GetItemValue(SPListItem item, string fieldName, string defaultValue)
        {
            return GetItemValue<string>(item, fieldName, defaultValue);
        }
        #endregion
    }
}
